from src.baseapp.controller.baseController import BaseController
from src.baseapp.model.testcaseServiceModel.versionModel import VersionModel
from src.baseapp.model.BaseOrmOpeator import OrmOperator


class VersionController(BaseController):
    def __init__(self):
        super().__init__()
        self.orm = OrmOperator(VersionModel)

    def add_version(self, project_id: int, version_info: dict):
        """
        添加版本
        :param project_id: 项目id
        :param version_info: 版本信息
        :return:
        """
        response_data = self.response_data()
        try:
            version = VersionModel(project_id=project_id, **version_info)
            self.orm.insert_data(version)
        except Exception as e:
            response_data.code = 500
            response_data.message = str(e)
        return response_data.code, response_data.to_dict

    def __delete_version(self, version_id: int):
        """
        删除版本
        :param version_id:
        """
        try:
            version = self.orm.equal(VersionModel.id, version_id).equal(VersionModel.is_delete, 0).get_one()
            version.is_delete = 1
            self.orm.update_data(version)
        except Exception as e:
            raise e

    def delete_version(self, version_id: int):
        """
        删除版本
        :param version_id:
        """
        response_data = self.response_data()
        try:
            self.__delete_version(version_id)
        except Exception as e:
            response_data.code = 500
            response_data.message = str(e)
        return response_data.code, response_data.to_dict

    def delete_all_version(self, project_id: int):
        """
        删除项目下所有版本
        :param project_id:
        """
        response_data = self.response_data()
        try:
            versions = self.orm.equal(VersionModel.project_id, project_id).equal(VersionModel.is_delete, 0).get_all()
            for version in versions:
                version.is_delete = 1
            self.orm.update_data(versions)
        except Exception as e:
            response_data.code = 500
            response_data.message = str(e)
        return response_data.code, response_data.to_dict

    def update_version(self, version_id: int, version_info: dict):
        """
        更新版本
        :param version_id:
        :param version_info:
        """
        response_data = self.response_data()
        try:
            version = self.orm.equal(VersionModel.version_id, version_id).equal(VersionModel.is_delete, 0).get_one()
            for key, value in version_info.items():
                setattr(version, key, value)
            self.orm.update_data(version)
        except Exception as e:
            response_data.code = 500
            response_data.message = str(e)
        return response_data

    def get_version_list(self, project_id: int, conditions: dict):
        """
        获取版本列表
        :param project_id:
        :param conditions:
        """
        field_conditions_mapping = {
            "version": VersionModel.version,
            "description": VersionModel.description,
            "status": VersionModel.version_status,
            "create_time": VersionModel.create_time,
            "update_time": VersionModel.update_time,
        }
        response_data = self.response_data()
        try:
            self.orm = self.orm.equal(VersionModel.project_id, project_id).equal(VersionModel.is_delete, 0)
            for con_key, con_value in conditions.items():
                if "status" == con_key:
                    self.orm = self.orm.in_here(field_conditions_mapping[con_key], con_value)
                elif con_key in ("create_time", "update_time"):
                    self.orm = self.orm.between(field_conditions_mapping[con_key], con_value[0], con_value[1])
                else:
                    self.orm = self.orm.like(field_conditions_mapping[con_key], con_value)
            versions = self.orm.get_all()
            response_data["versions"] = [
                {
                    "version_id": version.version_id,
                    "project_id": version.project_id,
                    "status": version.version_status,
                    "create_time": version.create_time,
                    "update_time": version.update_time,
                }
                for version in versions
            ]
        except Exception as e:
            response_data.code = 500
            response_data.message = str(e)
        return response_data.code, response_data.to_dict

    def get_version_by_id(self, version_id):
        """
        获取版本信息
        :param version_id:
        """
        response_data = self.response_data()
        try:
            version = self.orm.equal(VersionModel.version_id, version_id).equal(VersionModel.is_delete, 0).get_one()
            response_data.data["version_info"] = {
                "version_id": version.version_id,
                "project_id": version.project_id,
                "description": version.description,
                "status": version.version_status,
                "create_time": version.create_time,
                "update_time": version.update_time
            }
        except Exception as e:
            response_data.code = 500
            response_data.message = str(e)
        return response_data.code, response_data.to_dict
